/**
 * 
 */
package metalink.admin;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;

import javax.servlet.http.HttpSession;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

import vo.ResidentialRoomVo;

/**
 * @author Sony
 *
 */
public class ReadRoomListFromExcel extends ActionSupport{
	File excel_file;
	ArrayList<ResidentialRoomVo> room_list=new ArrayList<ResidentialRoomVo>();
	

	public ArrayList<ResidentialRoomVo> getRoom_list() {
		return room_list;
	}

	public void setRoom_list(ArrayList<ResidentialRoomVo> room_list) {
		this.room_list = room_list;
	}

	public File getExcel_file() {
		return excel_file;
	}

	public void setExcel_file(File excel_file) {
		this.excel_file = excel_file;
	}

	public String execute(){
		try{
			File f=new File("temp.csv");
			FileUtils.copyFile(excel_file, f);
			FileReader fr=new FileReader(f);
			BufferedReader br=new BufferedReader(fr);
			String str="";
			while(null!=(str=br.readLine())){
				System.out.println(str);
				String[] values=str.split(",");
				ResidentialRoomVo vo=new ResidentialRoomVo();
				vo.setResidential_id(Integer.parseInt(values[0]));
				vo.setRoom_number(Integer.parseInt(values[1]));
				vo.setOccupancy(Integer.parseInt(values[2]));
				vo.setVacancy(Integer.parseInt(values[3]));
				vo.setDescription(values[4]);
				room_list.add(vo);
			}
			HttpSession session = ServletActionContext.getRequest().getSession();
			session.setAttribute("uploaded_room_list",room_list);
			return SUCCESS;
		}
		catch(Exception e){
			e.printStackTrace();
			return ERROR;
		}
	}
}
